<?php
/**
 * Product images model.  
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 *
 */
class Models_Products_Images extends Bel_Model_Abstract {

	/**
	 * Array of many-to-one references with products  & images.
	 *
	 * @var array
	 */	
	protected $_referenceMap = array (
								'Product' => array (
									'columns' => 'product_id',
									'refTableClass' => 'Models_Products',
									'refColumns' => 'product_id', 
									'onDelete' => self::CASCADE	)
								);
    /**
     * Product ID. Used in _selectQuery method. Could be used
     * to store product id to be able to use it further in this class
     * methods
     *
     * @var integer
     */  	
	private $_product_id = null;								

	/**
	 * Constructor. Sets table name & primary key
	 *
	 * @return void
	 */
	public function __construct() {
		$this->_name = TABLE_PREFIX . "product_images";
		$this->_primary = array('image_id');
		parent::__construct ();
	}

	/**
	 * Set $_product_id
	 *
	 * @param integer $product_id
	 */		
	public function setProduct($product_id){
		$this->_product_id = $product_id;
	}
	
	/**
	 * Reset is_main flag to 0 for all images af specified product 
	 *
	 * @param integer $product_id
	 * @return integer - number of affected rows
	 */
	public function resetMain($product_id){
		return $this->update(array('is_main'=>0),'product_id='.$product_id);
	}
	
	/**
	 * Prepares select query for getAll() method to select images assigned to product.
	 * Product id is set via setProduct
	 *
	 * @return Zend_Db_Select
	 */	
	protected function _selectQuery(){
		$select = parent::_selectQuery();
		if($this->_product_id){
			$select->where('product_id=\''.$this->_product_id.'\'');
		}
		
		return $select;
	}
}